#ifndef __WMUTILS_H__
#define __WMUTILS_H__

//-----------------some common C header file---------------------------//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cstdarg>
#include <ctime>

//-----------------some common STL header file-------------------------//
#include <iostream> 
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

//--------------------------declare namespace ------------------------//
using namespace std;

//-----------------some own header files -----------------------------//
#include "wf_type.h"
//-------------------------  some macro variable and type  -----------//    
const int MSGLEN=1024;
const int BUFLEN=256;
const int TIMELEN=12;
const int FULLTIMELEN=30;
const int DAYTIMES=24*60*60;
const int HOSTLEN=64;
const int DATETIMELEN=20;
const int WAIT_SECONDS=3;
const int ONEDAYSECONDS=86400;

#ifndef CERR
#define CERR std::cerr <<"\n["<<time(NULL)<<"]\t"<< __FILE__ << "," << std::dec << __LINE__ << " : "
#endif

#ifndef COUT
#define COUT std::cout <<"\n["<<time(NULL)<<"]\t"<< __FILE__ << "," << std::dec << __LINE__ << " : "
#endif


#ifndef COND_RETURN
#define COND_RETURN(cond,msg,ret) if(cond)\
        {\
            err_trace(msg);\
            return ret;\
        }
#endif

#ifndef COND_RETURN_VOID
#define COND_RETURN_VOID(cond,msg) if(cond)\
    {\
        err_trace(msg);\
        return;\
    }    
#endif


#ifndef SAFE_DELETE
#define SAFE_DELETE(p) \
    do \
    { \
        if(NULL != p) \
        { \
            delete p; \
            p = NULL; \
        } \
    } while(0)
#endif

#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) \
    do \
    { \
        if(NULL != p) \
        {\
            delete [] p; \
            p = NULL; \
        } \
    } while(0)
#endif

#ifndef SAFE_ADDREF
#define SAFE_ADDREF(p) if(p)\
    {\
        p->AddRef();\
    }

#endif

#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) if(p)\
    {\
        p->Release();\
    }
    
#endif

#ifdef COND_RETURN
#define NULL_RETURN(val,msg,ret) COND_RETURN(val==NULL,msg,ret)
#define NEGA_RETURN(val,msg,ret) COND_RETURN(val<0,msg,ret)
#endif

#define msg_trace(msg) {\
	char buf[30];\
	cout<<get_cur_time(buf, 30)<<" ["<<__FILE__<<","<<__LINE__<<"]\t"<<msg<<endl;\
}
#define var_trace(var) {\
	char buf[30];\
	cout<<get_cur_time(buf, 30)<<" ["<<__FILE__<<","<<__LINE__<<"]\t"<<#var<<"="<<var<<endl;\
}
#define WFASSERT(cond) {\
	if(!(cond)){ \
		cout<<"Assertion failed: "<<__FILE__<<","<<__LINE__<<": "<<#cond<<endl;\
		exit(EXIT_FAILURE); \
	}\
}
#define COMPILE_ASSERT(cond)   {\
	typedef char msg[(cond) ? 1 : -1]\
}

char* get_cur_time(char* buffer, int len=30) 
{
	time_t rawtime;
	struct tm * timeinfo;

	time(&rawtime );
	timeinfo = localtime(&rawtime );

	strftime (buffer,30,"%x %X\0",timeinfo);
	return buffer;

}

#endif /*_WMUTILS_H_*/
